package com.ly.algorithm.DataStructures.stack;

/**
 * @Classname LinkedStack
 * @Description TODO
 * @Date 2020/4/22 16:20
 * @Author 冷心影翼
 */
public class LinkedStack {

    private Node head;

    private int maxSize;


    public LinkedStack(int maxSize) {
        head = new Node(0);
        this.maxSize =maxSize;
    }

    public boolean isEmpty() {
        if(head.next == null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isFull() {
        Node cur = head.next;
        int count = 0;
        while (true) {
            if(cur==null) {
                break;

            }
            cur = cur.next;
            count++;
        }
        if(count == maxSize) {
            return true;
        }else {
            return false;
        }
    }

    public void push(Node n) {
        if(isFull()) {
            throw new RuntimeException("栈溢出");
        }
        Node temp = head.next;
        head.next = n;
        n.next = temp;
    }

    public Node pop() {
        if(isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        Node n = head.next;
        head.next = head.next.next;
        return n;
    }

    public void list() {
        Node cur = head.next;
        while (true) {
            if(cur == null) {
                break;
            }
            System.out.println(cur);
            cur = cur.next;
        }
    }
}

class Node{
    public int data;

    public Node next;

    public Node(int data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                ", next=" + next +
                '}';
    }
}

class Test2 {

    public static void main(String[] args) {
        LinkedStack linkedStack = new LinkedStack(3);
        linkedStack.push(new Node(1));
        linkedStack.push(new Node(2));
        linkedStack.push(new Node(3));
        linkedStack.pop();

        linkedStack.list();
    }
}
